package controllers;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.LinkModel;

import data.CategoryData;
import data.LinkData;
import debug.LinkDebug;

public class PublishLinkServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		response.setContentType("application/json");
		CategoryData cData = new CategoryData();
		LinkData linkData = new LinkData();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		long linkId = 0;
		
		try {
			linkId = Long.parseLong(URLDecoder.decode(path.toLowerCase().replaceAll("/publish-link/", ""),"UTF-8"));
		} catch (Exception e) {
			response.sendRedirect("/home");
		}
		
		LinkModel link = linkData.getLink(linkId);
		if (link != null) {
			link = linkData.updatePublished(link);
			response.getWriter().println(link.getStatus());
		}
	}
}
